<?php ob_start();
session_start(); //on relaye la session
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
	$enregistrer = $_SESSION["is_registered"];
	$message="";
}else{
	header("Location:../../menu.php?erreur=intru"); // redirection en cas d'echec
}?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Recherche de personne</title>
</head>
<?php 
	require_once('../../Connections/microassur.php'); 
	require_once('../../utils/fonctions.php');	
	$message = '';
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'%" . $theValue . "%'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];


$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

$where = "";

if ((isset($_POST["MM_rechercher"])) && ($_POST["MM_rechercher"] == "form1")) {
	if(isset($_POST['Type_personne']) && !empty($_POST['Type_personne']))
		$where .= " AND Type_personne LIKE " . GetSQLValueString($_POST['Type_personne'], "text");
	if(isset($_POST['Nom_personne']) && !empty($_POST['Nom_personne']))
		$where .= " AND Nom_personne LIKE " . GetSQLValueString($_POST['Nom_personne'], "text");
	if(isset($_POST['Prenom_personne']) && !empty($_POST['Prenom_personne']))
		$where .= " AND Prenom_personne LIKE " . GetSQLValueString($_POST['Prenom_personne'], "text");
	if(isset($_POST['Date_naissance_personne']) && !empty($_POST['Date_naissance_personne']))
		$where .= " AND Date_naissance_personne = " . GetSQLValueString(dateEN2FR($_POST['Date_naissance_personne']), "date");
}


$maxRows_rs1 = 10;
$pageNum_rs1 = 0;
if (isset($_GET['pageNum_rs1'])) {
  $pageNum_rs1 = $_GET['pageNum_rs1'];
}
$startRow_rs1 = $pageNum_rs1 * $maxRows_rs1;

mysql_select_db($database_microassur, $microassur);
	$query_rs1 = "SELECT * FROM personne WHERE Etat_personne = 'A' " . $where ;
$query_limit_rs1 = sprintf("%s LIMIT %d, %d", $query_rs1, $startRow_rs1, $maxRows_rs1);
$rs1 = mysql_query($query_limit_rs1, $microassur) or die(mysql_error());
$row_rs1 = mysql_fetch_assoc($rs1);

if (isset($_GET['totalRows_rs1'])) {
  $totalRows_rs1 = $_GET['totalRows_rs1'];
} else {
  $all_rs1 = mysql_query($query_rs1);
  $totalRows_rs1 = mysql_num_rows($all_rs1);
}
$totalPages_rs1 = ceil($totalRows_rs1/$maxRows_rs1)-1;

$queryString_rs1 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_rs1") == false && 
        stristr($param, "totalRows_rs1") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_rs1 = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_rs1 = sprintf("&totalRows_rs1=%d%s", $totalRows_rs1, $queryString_rs1);
?>

<body>
<?php 
	require('../index.php');
	if(isset($message) && !empty($message)){
		echo $message;
	}
?>
<table align="center" width="700px">
	<tr>
    	<td>
			<h3>Production</h3>        
            <hr />
            <h5 style="text-align:center">RECHERCHE DE PERSONNE(S)</h5>
            <hr />
        </td>
    </tr>
</table>

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <table align="center" border="1">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">TYPE :</td>
      <td>
      <select name="Type_personne">
        <option value="M" <?php echo remind_select('Type_personne', 'M') ?>>Morale</option>
        <option value="P" <?php echo remind_select('Type_personne', 'P') ?>>Physique</option>
      </select>
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">NOM :</td>
      <td><input type="text" name="Nom_personne" value="<?php echo remind_text('Nom_personne'); ?>" size="32" maxlength="30" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">PRENOM :</td>
      <td><input type="text" name="Prenom_personne" value="<?php echo remind_text('Prenom_personne'); ?>" size="32" maxlength="30" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">DATE NAISSANCE :</td>
      <td><input type="text" name="Date_naissance_personne" value="<?php echo remind_text('Date_naissance_personne'); ?>" size="10" />&nbsp;(jj-mm-aaaa)</td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td>
      	<input type="submit" value="Rechercher" />&nbsp;
        <a href="#">Retour</a>
      </td>
    </tr>
  </table>
  <input type="hidden" name="Identifiant_personne" value="" />
  <input type="hidden" name="MM_rechercher" value="form1" />
</form>
<br />

<table border="1" align="center">
  <tr>
    <td>NOM</td>
    <td>PRENOM</td>
    <td>CIVILITE</td>
    <td>RAISON SOCIALE</td>
    <td>DATE NAISSANCE</td>
    <td>LIEU NAISSANCE</td>
    <td>SEXE</td>
    <td>PROFESSION</td>
    <td>EMPLOYEUR</td>
  </tr>
  <?php do {
  	if(isset($row_rs1['Type_personne']) && !empty($row_rs1['Type_personne'])){
  ?>
    <tr>
      <td><?php echo $row_rs1['Nom_personne']; ?></td>
      <td><?php echo $row_rs1['Prenom_personne']; ?></td>
      <td><?php echo $row_rs1['Civilite_personne']; ?></td>
      <td><?php echo $row_rs1['Raison_sociale']; ?></td>
      <td><?php echo $row_rs1['Date_naissance_personne']; ?></td>
      <td><?php echo $row_rs1['Lieu_naissance_personne']; ?></td>
      <td><?php echo $row_rs1['Sexe_personne']; ?></td>
      <td><?php echo $row_rs1['Profession_personne']; ?></td>
      <td><?php echo $row_rs1['Employeur_personne']; ?></td>
      <td><a href="saisiePersonne.php?Identifiant_personne=<?php echo $row_rs1['Identifiant_personne'] ?>">Valider</a></td>
    </tr>
    <?php 
	}
	} while ($row_rs1 = mysql_fetch_assoc($rs1)); ?>
</table>


<table border="0" align="center">
  <tr>
    <td><?php if ($pageNum_rs1 > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_rs1=%d%s", $currentPage, 0, $queryString_rs1); ?>">First</a>
          <?php } // Show if not first page ?>
    </td>
    <td><?php if ($pageNum_rs1 > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_rs1=%d%s", $currentPage, max(0, $pageNum_rs1 - 1), $queryString_rs1); ?>">Previous</a>
          <?php } // Show if not first page ?>
    </td>
    <td><?php if ($pageNum_rs1 < $totalPages_rs1) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_rs1=%d%s", $currentPage, min($totalPages_rs1, $pageNum_rs1 + 1), $queryString_rs1); ?>">Next</a>
          <?php } // Show if not last page ?>
    </td>
    <td><?php if ($pageNum_rs1 < $totalPages_rs1) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_rs1=%d%s", $currentPage, $totalPages_rs1, $queryString_rs1); ?>">Last</a>
          <?php } // Show if not last page ?>
    </td>
  </tr>
</table>

<p align="center">
<?php $url = "index.php";
if (isset($_SERVER['QUERY_STRING'])) {
  $url .= "?" . htmlentities($_SERVER['QUERY_STRING']."&recherche=0");
}?>
<a href="<?php echo $url;?>">Retour</a></p>
</body>
</html>
<?php
mysql_free_result($rs1);
?>
<?php ob_end_flush();?>